/*
 * Copyright (c) 2009. Silenus Consultoria, S.L.
 */
package es.silenus.detecta.controller;

import es.silenus.detecta.Constants;

import es.silenus.detecta.dao.CouncilDAO;
import es.silenus.detecta.dao.CouncilGroupDAO;
import es.silenus.detecta.dao.Page;

import es.silenus.detecta.model.Council;
import es.silenus.detecta.model.CouncilGroup;
import es.silenus.detecta.model.User;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * Council group controller.
 *
 * @author <a href="mailto:malonso@silenus-consultoria.es">Mariano Alonso</a>
 *
 * @since 09-may-2009 17:21:39
 */
public class CouncilGroupController extends BaseController {
	/**
	 * The council group DAO.
	 */
	private CouncilGroupDAO councilGroupDAO;

	/**
	 * The council DAO.
	 */
	private CouncilDAO councilDAO;

	/**
	 * Sets the council group DAO.
	 *
	 * @param councilGroupDAO the council group DAO.
	 */
	public void setCouncilGroupDAO(final CouncilGroupDAO councilGroupDAO) {
		this.councilGroupDAO = councilGroupDAO;
	}

	/**
	 * Sets the council DAO.
	 *
	 * @param councilDAO the council DAO.
	 */
	public void setCouncilDAO(final CouncilDAO councilDAO) {
		this.councilDAO = councilDAO;
	}

	/**
	 * Updates a council group.
	 *
	 * @param request the request.
	 * @param response the response.
	 *
	 * @return the view name.
	 */
	public String updateCouncilGroup(HttpServletRequest request, HttpServletResponse response) {
		final User current = this.getUser(request);

		String id = request.getParameter("id");

		long groupId = (id != null) ? Long.parseLong(id) : 0;

		CouncilGroup group;

		if(groupId > 0) {
			group = councilGroupDAO.findById(groupId);
		} else {
			group = new CouncilGroup();
		}

		group.setOrganization(current.getOrganization());

		String temp = request.getParameter("name");

		if(temp != null) {
			group.setName(temp);

			CouncilGroup otherGroup = councilGroupDAO.findByName(temp, current.getOrganization().getId());

			if((otherGroup != null) && (otherGroup.getId() != groupId)) {
				request.setAttribute(Constants.ERRORS, "councilgroup.name.duplicated");

				return "secure/organization/council-group-update-error";
			}
		}

		if(groupId > 0) {
			councilGroupDAO.update(group);
		} else {
			councilGroupDAO.save(group);
		}

		return "secure/organization/council-group-updated";
	}

	/**
	 * Finds council groups.
	 *
	 * @param request the request.
	 * @param response the response.
	 *
	 * @return the view name.
	 */
	public String findCouncilGroup(final HttpServletRequest request, final HttpServletResponse response) {
		final User current = this.getUser(request);

		Map<String, Object> sample = new HashMap<String, Object>();
		String temp = request.getParameter("id");

		if(temp != null) {
			sample.put("id", temp);
		}

		temp = request.getParameter("name");

		if(temp != null) {
			sample.put("name", temp);
		}

		sample.put("organization.id", current.getOrganization().getId());

		int start = Integer.parseInt(request.getParameter("start"));
		int limit = Integer.parseInt(request.getParameter("limit"));
		String[] sortFields = request.getParameterValues("sort");
		String[] sortDirections = request.getParameterValues("dir");

		Page<CouncilGroup> page = councilGroupDAO.findByExample(sample, start, limit, sortFields, sortDirections);

		request.setAttribute("page", page);

		return "secure/organization/council-group-search";
	}

	/**
	 * Retrieves council groups.
	 *
	 * @param request the request.
	 * @param response the response.
	 *
	 * @return the view name.
	 */
	public String getCouncilGroups(final HttpServletRequest request, final HttpServletResponse response) {
		final User current = this.getUser(request);

		String query = request.getParameter("query");

		List<CouncilGroup> result;

		if((query != null) && (query.length() > 0)) {
			Map<String, Object> sample = new HashMap<String, Object>();

			sample.put("name", query);
			sample.put("organization.id", current.getOrganization().getId());

			result = councilGroupDAO.findByExample(sample);
		} else {
			result = councilGroupDAO.findByOrganization(current.getOrganization().getId());
		}

		request.setAttribute("result", result);

		return "secure/organization/council-groups";
	}

	/**
	 * Finds associated councils to a given council group.
	 *
	 * @param request the request.
	 * @param response the response.
	 *
	 * @return the view name.
	 */
	public String findAssociatedCouncils(final HttpServletRequest request, final HttpServletResponse response) {
		final User current = this.getUser(request);

		long id = Long.parseLong(request.getParameter("id"));

		Map<String, Object> sample = new HashMap<String, Object>();

		sample.put("id", id);
		sample.put("organization.id", current.getOrganization().getId());

		int start = Integer.parseInt(request.getParameter("start"));
		int limit = Integer.parseInt(request.getParameter("limit"));
		String[] sortFields = request.getParameterValues("sort");
		String[] sortDirections = request.getParameterValues("dir");


		Page<Council> page = councilGroupDAO.findAssociatedCouncils(sample, start, limit, sortFields, sortDirections);

		request.setAttribute("page", page);


		return "secure/organization/council-search";
	}

	/**
	 * Finds non associated councils to a given council group.
	 *
	 * @param request the request.
	 * @param response the response.
	 *
	 * @return the view name.
	 */
	public String findNonAssociatedCouncils(final HttpServletRequest request, final HttpServletResponse response) {
		final User current = this.getUser(request);

		long id = Long.parseLong(request.getParameter("id"));

		Map<String, Object> sample = new HashMap<String, Object>();

		sample.put("id", id);
		sample.put("organization.id", current.getOrganization().getId());

		int start = Integer.parseInt(request.getParameter("start"));
		int limit = Integer.parseInt(request.getParameter("limit"));
		String[] sortFields = request.getParameterValues("sort");
		String[] sortDirections = request.getParameterValues("dir");


		Page<Council> page = councilGroupDAO.findNonAssociatedCouncils(sample, start, limit, sortFields, sortDirections);

		request.setAttribute("page", page);

		return "secure/organization/council-search";
	}

	/**
	 * Adds a council to a council group.
	 *
	 * @param request the request.
	 * @param response the response.
	 *
	 * @return the view name.
	 */
	public String addCouncil(final HttpServletRequest request, final HttpServletResponse response) {
		final User current = this.getUser(request);

		long id = Long.parseLong(request.getParameter("id"));

		String[] councils = request.getParameterValues("councils");

		if(councils != null) {
			CouncilGroup group = councilGroupDAO.findById(id);
			List<Council> theCouncils = councilDAO.findByIds(councils);

			group.getCouncils().addAll(theCouncils);
			councilGroupDAO.update(group);
		}

		return "secure/organization/council-group-added";
	}

	/**
	 * Removes a council from a council group.
	 *
	 * @param request the request.
	 * @param response the response.
	 *
	 * @return the view name.
	 */
	public String removeCouncil(final HttpServletRequest request, final HttpServletResponse response) {
		final User current = this.getUser(request);

		long id = Long.parseLong(request.getParameter("id"));
		String[] councils = request.getParameterValues("councils");

		if(councils != null) {
			CouncilGroup group = councilGroupDAO.findById(id);
			List<Council> theCouncils = councilDAO.findByIds(councils);

			group.getCouncils().removeAll(theCouncils);
			councilGroupDAO.update(group);
		}

		return "secure/organization/council-group-removed";
	}

	/**
	 * Checks remove councils groups.
	 *
	 * @param request the request.
	 * @param response the response.
	 *
	 * @return the view name.
	 */
	public String checkDeleteCouncilgroups(final HttpServletRequest request, final HttpServletResponse response) {
		String[] councilgroups = request.getParameterValues("elements");

		if(councilgroups != null) {
			Long[] councilgroupIds = new Long[councilgroups.length];

			for(int i = 0; i < councilgroups.length; ++i) {
				councilgroupIds[i] = Long.parseLong(councilgroups[i]);
			}

			List theCouncilgroups = councilGroupDAO.getRemovalInformation(councilgroupIds);

			request.setAttribute("councilgroups", theCouncilgroups);
		}

		return "secure/organization/council-groups-check-removed";
	}

	/**
	 * Remove councils groups.
	 *
	 * @param request the request.
	 * @param response the response.
	 *
	 * @return the view name.
	 */
	public String deleteCouncilgroups(final HttpServletRequest request, final HttpServletResponse response) {
		String[] councilgroups = request.getParameterValues("elements");

		if(councilgroups != null) {
			Long[] councilgroupIds = new Long[councilgroups.length];

			for(int i = 0; i < councilgroups.length; ++i) {
				councilgroupIds[i] = Long.parseLong(councilgroups[i]);
			}

			councilGroupDAO.remove(councilgroupIds);
		}

		return "secure/organization/council-groups-removed";
	}
}
